package com.hexacta.hrs.report.managers;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.vectrics.common.util.DateUtil;
import org.vectrics.employee.Employee;

import com.hexacta.hrs.report.AbstractReport;
import com.hexacta.hrs.report.dtos.EmployeeCareerReportDTO;
import com.hexacta.hrs.services.employee.EmployeeService;

/**
 * @author iandronowicz clase que genera el Employee Career Report
 */
public class EmployeeCarreerReport extends AbstractReport {
    
    private EmployeeService employeeService;

    /**
     * @return the employeeService
     */
    public EmployeeService getEmployeeService() {
        return employeeService;
    }

    /**
     * @param employeeService
     *            the employeeService to set
     */
    public void setEmployeeService(final EmployeeService employeeService) {
        this.employeeService = employeeService;
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.hexacta.hrs.report.Report#createPDFReport(java.lang.String,
     * java.lang.String)
     */
    public ByteArrayOutputStream createPDFReport(final String reportName, final String reportTemplateLocation,
            final Map<String, Object> filters, final Map<String, Object> parameters) {
    	if (reportName.equalsIgnoreCase("Active Employee Carreer Report")) {
			filters.put("Active", Boolean.TRUE);
		}
        return createPDF(reportTemplateLocation, parameters, getReportData(filters));
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.hexacta.hrs.report.Report#createXLSReport(java.lang.String,
     * java.lang.String)
     */
    public ByteArrayOutputStream createXLSReport(final String reportName, final String reportTemplateLocation,
            final Map<String, Object> filters, final Map<String, Object> parameters) {
    	
    	if (reportName.equalsIgnoreCase("Active Employee Carreer Report")) {
			filters.put("Active", Boolean.TRUE);
		}
    	
        return createExcel(reportTemplateLocation, parameters, getReportData(filters));
    }

    @Override
    @SuppressWarnings("unchecked")
    protected Collection getReportData(final Map<String, Object> filters) {
        Collection<EmployeeCareerReportDTO> collection = new ArrayList<EmployeeCareerReportDTO>();
        
        Collection<Employee> employeeList;
        if(filters != null && filters.containsKey("Active")){
        	employeeList = employeeService.getAllCurrentEmployees();
        }else{
        	employeeList = employeeService.getAll();
        }
        
        for (Employee employee : employeeList) {
            EmployeeCareerReportDTO dto = new EmployeeCareerReportDTO();

            if (employee.getPerson() != null) {
            	dto.setCuil(employee.getPerson().getCuilOrCuit());
                dto.setFirstName(employee.getPerson().getFirstName());
                dto.setLastName(employee.getPerson().getLastName());
            }

            if (employee.getFunction() != null) {
                dto.setCareerRole(employee.getFunction().getDescription());
            }

            if (employee.getCurrentStatus() != null && employee.getCurrentStatus().getStatusType() != null) {
                dto.setCondition(employee.getCurrentStatus().getStatusType().getDescription());
            }

            /*
             * Se pone el getDate Start ya que el dia que se utiliza para saber
             * cuando empezo no es cuando paso a empleado sino desde que entro a
             * HX.
             */
            if (employee.getDateStart() != null) {
                dto.setFirstWorkinDay(DATE_FORMAT.format(employee.getDateStart()).toString());
                
                int days = DateUtil.getDaysBetween(employee.getDateStart(), DateUtil.getCurrentDate());
                dto.setExperience(String.format(
                        EmployeeCareerReportDTO.EXPERIENCE_FORMAT, days / 365, days % 365 / 30));
            }
            
            if (employee.getFirstEmployeeDate() != null) {
                dto.setFirstEmployeeDay(DATE_FORMAT.format(employee.getFirstEmployeeDate()));
            }
            
                        
            if (employee.getFirstEmployeeDate() != null) {
                dto.setFirstEmployeeDay(DATE_FORMAT.format(employee.getFirstEmployeeDate()));
            }
            
            if (employee.getDateEnd() != null) {
                dto.setLastWorkingDay(DATE_FORMAT.format(employee.getDateEnd()));
            }
            
            if (employee.getHoursPerDay() != null) {
                dto.setHoursPerDay(employee.getHoursPerDay().toString());
            }

            if (employee.getMentor() != null && employee.getMentor().getPerson() != null) {
                dto.setMentor(employee.getMentor().getPerson().getLastName() + ", " + 
                		employee.getMentor().getPerson().getFirstName() );
            }

            if (employee.getCareerRolePosition() != null) {
                dto.setCareerRolePosition(employee.getCareerRolePosition().toString());
            }

            if (employee.getLocation() != null) {
                dto.setOffice(employee.getLocation().getAbbreviation());
            }

            collection.add(dto);
        }

        if (collection.isEmpty())
            throw new RuntimeException("No data Available for Report");

        Collections.sort((List) collection);
        return collection;

    }

    @SuppressWarnings("unchecked")
    @Override
    protected Map getReportParameters() {
        // TODO Auto-generated method stub
        return null;
    }
}
